package cn.dankal.www.uitest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 苏彬 on 2016/3/14.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<HeadBean> headBean = new ArrayList<>();
    private List<BodyBean> bodyBean = new ArrayList<>();
    private List<FootBean> footBean = new ArrayList<>();
    private Context mContext;
    private OnItemOnClickListener onItemOnClickListener;

    // 用来控制CheckBox的选中状况
    private static HashMap<Integer, Boolean> isSelected;

    //需要两条分割线，头部和中部之间一条，中部和尾部之间一条
    private int dividerCount = 2;


    public RecyclerViewAdapter(Context mContext, List<HeadBean> headBean) {
        this.mContext = mContext;
        this.headBean = headBean;
    }

    public RecyclerViewAdapter(Context mContext, List<HeadBean> headBean, List<BodyBean> bodyBean) {
        this.mContext = mContext;
        this.headBean = headBean;
        this.bodyBean = bodyBean;
    }

    public RecyclerViewAdapter(Context mContext, List<HeadBean> headBean, List<BodyBean> bodyBean, List<FootBean> footBean) {
        this.mContext = mContext;
        this.headBean = headBean;
        this.bodyBean = bodyBean;
        this.footBean = footBean;
        isSelected = new HashMap<Integer, Boolean>();
    }

    // 初始化isSelected的数据
    public void initDate() {
        if (footBean.size() != 0) {
            for (int i = 0; i < footBean.size(); i++) {
                getIsSelected().put(i, false);
            }
        }
        notifyDataSetChanged();
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = null;
        RecyclerView.ViewHolder holer = null;
        switch (viewType) {
            case Constant.TYPE_HEAD:
                v = LayoutInflater.from(mContext).inflate(
                        R.layout.recyclerview_head, null);
                holer = new HeadHolder(v);
                break;
            case Constant.TYPE_BODY:
                v = LayoutInflater.from(mContext).inflate(
                        R.layout.recyclerview_body, null);
                holer = new BodyHolder(v);
                break;
            case Constant.TYPE_FOOT:
                v = LayoutInflater.from(mContext).inflate(
                        R.layout.recyclerview_foot, null);
                holer = new FootHolder(v);
                break;
            case Constant.TYPE_DIVIDER:
                v = LayoutInflater.from(mContext).inflate(
                        R.layout.recyclerview_divider, null);
                holer = new DividerHolder(v);
                break;

        }

        return holer;
    }

    /**
     * 因为item的position和数据的position不同，所以除了头部，不能直接带入position
     *
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)) {
            case Constant.TYPE_HEAD:
                HeadHolder headHolder = (HeadHolder) holder;
                headHolder.tv_title.setText(headBean.get(position).getTitle());
                headHolder.tv_content1.setText(headBean.get(position).getContent1());
                headHolder.tv_content2.setText(headBean.get(position).getContent2());
                headHolder.tv_content3.setText(headBean.get(position).getContent3());
                break;
            case Constant.TYPE_BODY:
                BodyHolder bodyHolder = (BodyHolder) holder;
                final int bodyHolderPositon = position - headBean.size() - 1;//因为中间夹着一条分割线所以再减1
                bodyHolder.iv_item_icon.setImageResource(bodyBean.get(bodyHolderPositon).getBodyItemIcon());
                bodyHolder.tv_item_name.setText(bodyBean.get(bodyHolderPositon).getBodyItemName());
                bodyHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemOnClickListener.onItemClick(bodyHolderPositon);
                    }
                });
                break;
            case Constant.TYPE_FOOT:
                final FootHolder footHolder = (FootHolder) holder;
                final int footHolderPositon = position - headBean.size() - bodyBean.size() - dividerCount;
                footHolder.iv_foot_item_icon.setImageResource(footBean.get(footHolderPositon).getFootItemIcon());
                footHolder.tv_foot_item_name.setText(footBean.get(footHolderPositon).getFootItemName());
                footHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (isSelected.get(footHolderPositon)) {
                            isSelected.put(footHolderPositon, false);
                            setIsSelected(isSelected);
                        } else {
                            isSelected.put(footHolderPositon, true);
                            setIsSelected(isSelected);
                        }
                        footHolder.iv_select.setImageResource(getIsSelected().get(footHolderPositon) == true ? android.R.drawable.checkbox_on_background : android.R.drawable.checkbox_off_background);
                    }
                });
                footHolder.iv_select.setImageResource(getIsSelected().get(footHolderPositon) == true ? android.R.drawable.checkbox_on_background : android.R.drawable.checkbox_off_background);
                break;
            case Constant.TYPE_DIVIDER:
                break;

        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position < headBean.size()) {
            return Constant.TYPE_HEAD;
        }
        if (position > headBean.size() & position < (headBean.size() + bodyBean.size() + 1)) {//同样是分割线的原因+1
            return Constant.TYPE_BODY;
        }
        if (position > (headBean.size() + bodyBean.size() + 1)) {
            return Constant.TYPE_FOOT;
        }
        return Constant.TYPE_DIVIDER;

    }

    @Override
    public int getItemCount() {
        if (bodyBean.size() == 0 & footBean.size() == 0) {
            return headBean.size();
        }
        if (footBean.size() == 0) {
            return headBean.size() + bodyBean.size() + dividerCount;
        }
        return headBean.size() + bodyBean.size() + footBean.size() + dividerCount;
    }


    class HeadHolder extends RecyclerView.ViewHolder {
        TextView tv_title, tv_content1, tv_content2, tv_content3;

        public HeadHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_content1 = (TextView) itemView.findViewById(R.id.tv_content1);
            tv_content2 = (TextView) itemView.findViewById(R.id.tv_content2);
            tv_content3 = (TextView) itemView.findViewById(R.id.tv_content3);
        }
    }


    class BodyHolder extends RecyclerView.ViewHolder {
        ImageView iv_item_icon;
        TextView tv_item_name;

        public BodyHolder(View itemView) {
            super(itemView);
            iv_item_icon = (ImageView) itemView.findViewById(R.id.iv_item_icon);
            tv_item_name = (TextView) itemView.findViewById(R.id.tv_item_name);
        }
    }

    class FootHolder extends RecyclerView.ViewHolder {
        ImageView iv_foot_item_icon, iv_select;
        TextView tv_foot_item_name;

        public FootHolder(View itemView) {
            super(itemView);
            iv_foot_item_icon = (ImageView) itemView.findViewById(R.id.iv_foot_item_icon);
            iv_select = (ImageView) itemView.findViewById(R.id.iv_select);
            tv_foot_item_name = (TextView) itemView.findViewById(R.id.tv_foot_item_name);
        }
    }

    class DividerHolder extends RecyclerView.ViewHolder {

        public DividerHolder(View itemView) {
            super(itemView);

        }
    }

    public interface OnItemOnClickListener {
        void onItemClick(int position);
    }

    public void setOnBodyItemOnClickListener(OnItemOnClickListener onItemOnClickListener) {
        this.onItemOnClickListener = onItemOnClickListener;
    }

    public static HashMap<Integer, Boolean> getIsSelected() {
        return isSelected;
    }

    public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
        RecyclerViewAdapter.isSelected = isSelected;
    }
}
